var Tab = function () {
    return {
        elem: null,
        ELEM: {},
        init: function (options) {
            var that = this;
            that.elem = options.elem;
            if (typeof(that.elem) !== 'string' && typeof(that.elem) !== 'object') {
                console.log('Tab error: elem参数未定义或设置出错，具体设置格式请参考文档API.');
            }
            var $container;
            if (typeof(that.elem) === 'string') {
                $container = $('' + that.elem + '');
            }
            if (typeof(that.elem) === 'object') {
                $container = that.elem;
            }
            if ($container.length === 0) {
                console.log('Tab error:找不到elem参数配置的容器，请检查.');
            }
            that.elem = $container;
            that.ELEM.titleBox = $container.children('ul');
            that.ELEM.contentBox = $container.children('div.tab-content');
            return that;
        },
        exists: function (title) {
            var that = this;
            var tabIndex = -1;
            that.ELEM.titleBox.find('li').each(function (i) {
                var $a = $(this).children('a');
                if ($a.attr("title") === title) {
                    tabIndex = i;
                }
            });
            return tabIndex;
        },
        tabChange: function (title) {
            var that = this;
            var tabIndex = that.exists(title);
            if (tabIndex) {
                that.ELEM.titleBox.find("li.active").removeClass("active");
                that.ELEM.contentBox.find("div.active").removeClass("in active");
                that.ELEM.titleBox.find('li:eq(' + tabIndex + ')').addClass("active");
                that.ELEM.contentBox.find('div:eq(' + tabIndex + ')').addClass("active");
            }
        },
        tabAdd: function (data) {
            var that = this;
            var tabIndex = that.exists(data.title);
            if (tabIndex === -1) {
                var content = '<div id="' + data.id + '" class="tab-pane">' +
                    '<iframe data-id="' + data.id + '" src="' + data.href + '" scrolling="yes" frameborder="0"></iframe>' +
                    '</div>';

                var title = '<li>' +
                    '<a data-toggle="tab" href="#' + data.id + '" title="' + data.title + '">' + data.title + '<i style="margin-left: 5px" class="fa fa-close"></i></a>' +
                    '</li>';

                //添加tab
                that.ELEM.titleBox.append(title);
                that.ELEM.contentBox.append(content);

                //iframe 自适应
                that.ELEM.contentBox.find('iframe[data-id=' + data.id + ']').each(function () {
                    // $(this).width(that.ELEM.contentBox.width());
                    $(this).css("width", "100%");
                    $(this).height($(".page-content").height() - 55);
                });

                // 监听悬浮事件
                $(".fa-close").mouseover(function () {
                    $(this).css("background-color", "#ccc");
                    $(this).css("border", "1 solid #ccc");
                    $(this).css("border-radius", "20px");
                });

                // 监听离开事件
                $(".fa-close").mouseout(function () {
                    $(this).css("background-color", "");
                });

                // 监听关闭事件
                $(".fa-close").click(function () {
                    var activateTitleBox = $(this).parents("li");
                    var activateContentBox = $(activateTitleBox.find("a").attr("href"));

                    // 获取下一个元素
                    var readyTitleBox = activateTitleBox.next();
                    var readyContentBox = $(readyTitleBox.find("a").attr("href"));

                    // 获取失败，则获取上一个元素
                    if (readyTitleBox.length === 0) {
                        readyTitleBox = activateTitleBox.prev();
                        readyContentBox = $(readyTitleBox.find("a").attr("href"));
                    }

                    activateTitleBox.remove();
                    activateContentBox.remove();

                    that.tabChange(readyTitleBox.find("a").attr("href"));
                });

                //切换到当前打开的选项卡
                that.tabChange(data.title);
            } else {
                that.tabChange(data.title);
            }
        }
    }
};